/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ecom.beans.account;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 *
 * @author Tian
 */

@NamedQueries({
    
    @NamedQuery(name = "findAllAccounts",
    query = "SELECT account FROM AccountEntity account"),
            
    @NamedQuery(name = "findAccountById",
    query = "SELECT account FROM AccountEntity account WHERE account.id = :id"),
            
    @NamedQuery(name = "findAllAccountsByType",
    query = "SELECT account FROM AccountEntity account WHERE account.accountType = :accountType"), 
            
    @NamedQuery(name = "findAccountByIBAN",
    query = "SELECT account FROM AccountEntity account WHERE account.accountIBAN = :accountIBAN") 
})

@Entity
public class AccountEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    //private Long id;
    
    private String accountIBAN;
    private double accountBalance;
    private int accountType;
    
    public AccountEntity() {
        this.accountBalance = 0.0;
    }
    

    public AccountEntity(String accountIBAN, double accountBalance, int accountType) {
        this.setAccountIBAN(accountIBAN);
        this.setAccountBalance(accountBalance);
        this.setAccountType(accountType);
    }


    public void setId(Long id) {
        this.id = id;
    }

    
    public Long getId() {
        return id;
    }

    public double getAccountBalance() {
        return accountBalance;
    }

    public void setAccountBalance(double accountBalance) {
        this.accountBalance = accountBalance;
    }

    public String getAccountIBAN() {
        return accountIBAN;
    }

    public void setAccountIBAN(String accountIBAN) {
        this.accountIBAN = accountIBAN;
    }

    /**************************************************/
    // additional methods
    
    public void deposit(double amount){
        
        accountBalance += amount;
        
    }

    public int getAccountType() {
        return accountType;
    }

    public void setAccountType(int accountType) {
        this.accountType = accountType;
    }
    
    
    
    public boolean withdraw(double amount){
        
        if(accountBalance>=amount && accountBalance>0.0){
            accountBalance -= amount;
            return true;
        }
        else{ 
            return false;
        }
    }
    
    /**************************************************/
    
    @Override
    public int hashCode() {
        int hash = 0;
        
        
        hash += (id != null ? id.hashCode() : 0);

        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof AccountEntity)) {
            return false;
        }
        AccountEntity other = (AccountEntity) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ejb.account.AccountEntity[id=" + id + "]";
    }

}
